﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;

namespace GrayParrot.Configuration
{
    public class ConfigurationSectionEx : ConfigurationSection
    {
        private string m_connectionStringName = string.Empty;
        private string m_tableName = string.Empty;

        //public ConfigurationSectionEx()
        //{
        //    DatabaseConfigurationsectionAttribute MyAttribute = (DatabaseConfigurationsectionAttribute) Attribute.GetCustomAttribute(this.GetType(), typeof (DatabaseConfigurationsectionAttribute));
        //    if(MyAttribute != null)
        //    {
        //        m_connectionStringName = ((DatabaseConfigurationsectionAttribute)MyAttribute).ConnectionStringName;
        //    }
        //}

        private void GetConnectionStringName()
        {
            DatabaseConfigurationSectionAttribute MyAttribute = (DatabaseConfigurationSectionAttribute)Attribute.GetCustomAttribute(this.GetType(), typeof(DatabaseConfigurationSectionAttribute));
            if (MyAttribute != null)
            {
                m_connectionStringName = ((DatabaseConfigurationSectionAttribute)MyAttribute).ConnectionStringName;
                m_tableName = ((DatabaseConfigurationSectionAttribute)MyAttribute).TableName;
            }
        }

        protected override string SerializeSection(ConfigurationElement parentElement, string name, ConfigurationSaveMode saveMode)
        {
            GetConnectionStringName();

            string m_body = base.SerializeSection(parentElement, name, saveMode);

            string m_cn = ConfigurationManager.ConnectionStrings[m_connectionStringName].ConnectionString;

            using (SqlConnection conn = new SqlConnection(m_cn))
            {
                SqlCommand cmd = conn.CreateCommand();

                cmd.CommandText = string.Format("update {0} set section_body = @Body where section_name = @ConfigFileName", m_tableName);

                SqlParameter p = cmd.CreateParameter();
                p.ParameterName = "@ConfigFileName";
                p.Value = this.SectionInformation.SectionName;

                SqlParameter p1 = cmd.CreateParameter();
                p1.ParameterName = "@Body";
                p1.Value = m_body;

                cmd.Parameters.Add(p);
                cmd.Parameters.Add(p1);

                conn.Open();

                cmd.ExecuteNonQuery();

                return m_body;
            }
        }

        protected override void DeserializeSection(System.Xml.XmlReader reader)
        {
            GetConnectionStringName();

            string m_cn = ConfigurationManager.ConnectionStrings[m_connectionStringName].ConnectionString;

            using (SqlConnection conn = new SqlConnection(m_cn))
            {
                SqlCommand cmd = conn.CreateCommand();

                cmd.CommandText = string.Format("select section_body from {0} where section_name = @ConfigFileName", m_tableName);

                SqlParameter p = cmd.CreateParameter();
                p.ParameterName = "@ConfigFileName";
                p.Value = this.SectionInformation.SectionName;

                cmd.Parameters.Add(p);

                conn.Open();

                String xml = (String)cmd.ExecuteScalar();

                using (System.IO.StringReader sr = new System.IO.StringReader(xml))
                {
                    using (System.Xml.XmlReader xr = System.Xml.XmlReader.Create(sr))
                    {
                        base.DeserializeSection(xr);
                    }
                }
            }
        }
    }
}
